<?php defined('IN_LK') or die('Access Denied');
class image
{
	private $img  = null;
	private $str  = null;
	private $font = null;
	/**
	 * 图像背景
	 */
	private $background = '#F7FDF0';
	
	public function __construct($str = '')
	{
		$this->str = $str;
		$this->font = APP_ROOT . 'libs' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'font' . DIRECTORY_SEPARATOR . 'elephant.ttf';
		$this->create_image();
		$this->create_font();
	}
	
	/**
	 * 生成图片及背景
	 */
	private function create_image()
	{
		$len = strlen($this->str);
		if(!$len)
		{
			$len = 1;
		}
		$this->img  = imagecreatetruecolor(14 * $len + 10, 20);
		$background  = imagecolorallocate($this->img, 
										  hexdec(substr($this->background, 1, 2)), 
										  hexdec(substr($this->background, 3, 2)), 
										  hexdec(substr($this->background, 5, 2)));
		imagefill($this->img, 0, 0, $background);
	}
	
	/**
	 * 生成字符
	 */
	private function create_font()
	{
		$color = imagecolorallocate($this->img, 255, 0, 0);
		if(!is_file($this->font))
		{	
			imagestring($this->img, 5, 10, 3, $this->str, $color);
		}
		else
		{
			imagefttext($this->img, 14, 0, 10, 16, $color, $this->font, $this->str);
		}
	}
	
    /**
     * 输出图像
	 */
	public function output()
	{
		header('Content-type:image/jpeg');
		imagejpeg($this->img);
		imagedestroy($this->img);
	}
}